import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/entities/payslips_entity.dart';
import 'package:finance_app/src/utils/cash_utils.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'dart:math' as math;

class NormalBarOneWayChart extends StatefulWidget {
  final PayslipsEntity payslips;

  const NormalBarOneWayChart({Key key, this.payslips}) : super(key: key);

  @override
  _NormalBarOneWayChartState createState() => _NormalBarOneWayChartState();
}

class _NormalBarOneWayChartState extends State<NormalBarOneWayChart> {
  List<Color> colors = [
    aceBlueColor,
    aceBlueColor,
  ];

  num max = 0;

  @override
  void initState() {
    super.initState();
    initMax();
  }

  void initMax() {
    max = 0;
    widget.payslips.chartData.forEach((element) {
      max = math.max(element.value, max);
    });
  }

  @override
  Widget build(BuildContext context) {
    return AspectRatio(
      aspectRatio: 1.7,
      child: Padding(
        padding:
            EdgeInsets.only(top: getDp(6), left: getDp(11), right: getDp(43)),
        child: BarChart(
          BarChartData(
            maxY: max == 0 ? 250000 : null,
            minY: 0,
            groupsSpace: 24,
            barTouchData: BarTouchData(
              enabled: false,
            ),
            titlesData: FlTitlesData(
              show: true,
              bottomTitles: SideTitles(
                showTitles: true,
                getTextStyles: (_, value) => TextStyle(
                  color: aceBlackColor,
                  fontSize: getSp(12),
                  height: 14 / 12,
                ),
                margin: getDp(18),
                rotateAngle: 0,
                interval: 1,
                getTitles: (double value) {
                  if (value.toInt() % 2 == 0) {
                    return widget.payslips.chartData[value.toInt() ~/ 2].name;
                  } else {
                    return null;
                  }
                },
              ),
              leftTitles: SideTitles(
                showTitles: true,
                getTextStyles: (_, value) =>
                    TextStyle(color: aceBlackColor, fontSize: getSp(12)),
                rotateAngle: 0,
                getTitles: (double value) {
                  if (value == 0) {
                    return '\$0k';
                  }
                  if (value > 1000) {
                    return '\$${CashUtils.formatCashAmount(value / 1000)}k';
                  } else if (value > 10000) {
                    return '\$${value.toInt() ~/ 10000}w';
                  } else {
                    return '\$${value.toInt()}';
                  }
                },
                interval:
                    max == 0 ? 50000 : widget.payslips.interval?.toDouble(),
                margin: getDp(20),
                reservedSize: getDp(50),
              ),
              rightTitles: SideTitles(showTitles: false),
              topTitles: SideTitles(showTitles: false),
            ),
            gridData: FlGridData(
              show: true,
              checkToShowHorizontalLine: (value) => value % 50 == 0,
              getDrawingHorizontalLine: (value) {
                return FlLine(
                  color: aceWhiteGreyColor,
                  strokeWidth: getDp(1),
                );
              },
            ),
            borderData: FlBorderData(
              show: false,
            ),
            barGroups: List.generate(
                    widget.payslips.chartData.length,
                    (index) => _buildGroupData(
                        index * 2, widget.payslips.chartData[index].value))
                .toList(),
          ),
        ),
      ),
    );
  }

  BarChartGroupData _buildGroupData(num x, num y) => BarChartGroupData(
        x: x,
        barRods: [
          BarChartRodData(
            y: y?.toDouble(),
            width: getDp(12),
            colors: [y >= 0 ? colors[0] : colors[1]],
            borderRadius: const BorderRadius.only(
                topLeft: Radius.circular(2), topRight: Radius.circular(2)),
          ),
        ],
      );

  @override
  void didUpdateWidget(covariant NormalBarOneWayChart oldWidget) {
    setState(() {
      initMax();
    });
    super.didUpdateWidget(oldWidget);
  }
}
